package com.gitee.simons.oauth.entity.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author simon jiang
 * @time 2020/7/7 15:40
 */
@ApiModel("用户详细信息")
@Setter
@Slf4j
@AllArgsConstructor
public class SysUserDetails implements UserDetails {

    @ApiModelProperty("密码")
    private String password;

    @ApiModelProperty("账号")
    private String username;

    @ApiModelProperty("角色id列表")
    @Getter
    private List<String> roleCodes;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(roleCodes != null && !roleCodes.isEmpty()) {
            return this.roleCodes.stream().filter(Objects::nonNull).map(roleCode -> {
                return new SimpleGrantedAuthority(roleCode);
            }).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        log.info("--是否过--期操作暂时没加，要加可以在这里进行判断，默认全部返回true");
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        log.info("--上锁--操作暂时没加，要加可以在这里进行判断，默认全部返回true");
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        log.info("--凭证是否有效--操作暂时没加，要加可以在这里进行判断，默认全部返回true");
        return true;
    }

    @Override
    public boolean isEnabled() {
        log.info("--是否允许--操作暂时没加，要加可以在这里进行判断，默认全部返回true");
        return true;
    }
}
